package pl.enigmatic.input;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;


public class MouseValueIncreaser implements MouseListener, MouseMotionListener, MouseWheelListener {

	public static interface Target {

		void increaseValue(int steps);
	}

	private final Target trg;
	private boolean mouseOver = false;
	private final Point last = new Point();

	public MouseValueIncreaser(final Target trg) {
		this.trg = trg;
	}

	public void applyTo(final Component comp) {
		comp.addMouseListener(this);
		comp.addMouseWheelListener(this);
		comp.addMouseMotionListener(this);
	}

	@Override
	public void mouseWheelMoved(final MouseWheelEvent evt) {
		if (mouseOver) {
			trg.increaseValue(-evt.getWheelRotation());
		}
	}

	@Override
	public void mouseDragged(final MouseEvent evt) {
		trg.increaseValue(-evt.getPoint().y + last.y);
		last.setLocation(evt.getPoint());
	}

	@Override
	public void mouseMoved(final MouseEvent evt) {}

	@Override
	public void mouseClicked(final MouseEvent evt) {}

	@Override
	public void mouseEntered(final MouseEvent evt) {
		mouseOver = true;
	}

	@Override
	public void mouseExited(final MouseEvent evt) {
		mouseOver = false;
	}

	@Override
	public void mousePressed(final MouseEvent evt) {
		last.setLocation(evt.getPoint());
	}

	@Override
	public void mouseReleased(final MouseEvent evt) {}

}
